<?php

include_once("../includes/miscs.php");
include_once("../includes/system.php");
include_once("../includes/google.php");
include_once("../includes/version.php");
include_once("config.php");
include_once("app.php");

ini_set("display_errors", "1");
error_reporting(E_ALL);

$br = "<br/>";
$brnl = $br . "\r\n";
$ver = "00.02";

$config = new config();
$app = new app();

printHeaderWithLogo("Gsysteminfo", "Gsysteminfo", "info.png");

$action = "default";
if(isset($_REQUEST["action"])){
	$action = $_REQUEST["action"];
}

echo "<hr/>";

if($action == "netint"){

	//echo "EthTool" . $brnl;
	//printEthTool($config->interfaces_lan);
	//echo "Ip link" . $brnl;
	//printIpLink($config->interfaces_lan);

	$lan_nics = count($config->interfaces_lan);
	if($lan_nics>0){
		echo "Ip address" . $brnl;
		printIpAddr($config->interfaces_lan);
		echo "<hr/>";
	}

	$wlan_nics = count($config->interfaces_wlan);
	if($wlan_nics>0){
		echo "Wlan Ip address" . $brnl;
		printIpAddr($config->interfaces_wlan);
		echo "Wlan info" . $brnl;
		printWlanInfo($config->interfaces_wlan);
		echo "Wlan info 2" . $brnl;
		printWlanInfo2($config->interfaces_wlan);
		echo "<hr/>";
	}

	echo "Other" . $brnl;
	printOther();

} else if($action == "update"){
	printUpdate();
}else{
	printMain();
	echo "<hr/>";
	printRam();
	echo "<hr/>";
	printDiskSpace();
}

printMenu();


printFooter2($app->project_name . " (ver " . $app->version . ")", $app->project_home);

// #################################################################

function printEthTool($interfaces){
	global $brnl;
	foreach($interfaces as $int){
		echo "EthTool " . $int . $brnl;
		$cmd = "ethtool ". $int;
		$out = shell_exec($cmd);
		$array = explode("\n", $out);
		foreach($array as $line){
			echo $line . $brnl;
		}
	}
}

function printIpLink($interfaces){
	global $brnl;
	foreach($interfaces as $int){
		echo "Status of interface " . $int . $brnl;
		$cmd = "ip link show " . $int;
		$out = shell_exec($cmd);
		$array = explode("\n", $out);
		foreach($array as $line){
			echo $line . $brnl;
		}
	}
}

function printIpAddr($interfaces){
	global $brnl;
	foreach($interfaces as $int){
		echo "Status of interface " . $int . $brnl;
		$cmd = "ip addr show " . $int;
		$out = shell_exec($cmd);
		$array = explode("\n", $out);
		foreach($array as $line){
			echo $line . $brnl;
		}
	}
}

function printWlanInfo($interfaces_wlan){
	global $brnl;
	foreach($interfaces_wlan as $wlan){
		$cmd = "iwconfig " . $wlan;
		$out = shell_exec($cmd);
		$array = explode("\n", $out);
		foreach($array as $line){
			echo $line . $brnl;
		}
	}
}

function printWlanInfo2(){
	global $brnl;
	$cmd = "iwlist scan";
	$out = shell_exec($cmd);
	$array = explode("\n", $out);
	foreach($array as $line){
		echo $line . $brnl;
	}
}

function printOther(){
	global $brnl;
	$last = getLastTimeReboot();
	echo "Last reboot: " . $last . $brnl;
	echo "Mounted devices: " . $brnl;
	printMount();
}

function printMount(){
	global $brnl;
	$cmd = "mount";
	$out = shell_exec($cmd);
	$array = explode("\n", $out);
	foreach($array as $line){
		echo $line . $brnl;
	}
}



function printMenu(){
?>

<div class="clear" />

<hr />

<h2>Menu</h2>

<ul>
<li><a href="?action=update">Check if a new version is available</a></li>
<li><a href="?action=netint">Network info</a></li>
<li><a href="?action=main">Back to main page</a></li>
</ul>

<?php
}

function printUpdate(){
	global $app, $brnl;
	$version = new version();
	$version->update();
	echo "Home page: " . $version->gui_version . $brnl;
	echo "Installed version: " . $version->gui_version . $brnl;
	echo "Installed version date: " . $version->gui_date . $brnl;
	echo "Last version (available for download): " . $version->gui_last_version . $brnl;
	echo "Last version date: " . $version->gui_last_version_date . $brnl;

	if($version->needUpdate()){
		echo "Need update: download the new version from the <a href=\"" . $app->project_home . "\">project home page</a>" . $brnl;
	}
}

function printRam(){
	global $brnl;
	$columns = array();
	$columns["label1"] = "string";
	$columns["label2"] = "number";

	$cmd = "free";
	$temp = shell_exec($cmd);
	$devices = explode("\n", $temp);

	//print_r($devices);

	$volumes = array();
	for ($j=1 ; $j < count($devices) ; $j++){
		$tokens = preg_split("/[\s]+/", $devices[$j]);
		//print_r($tokens);
		if($j!=2){
		for ($i=0 ; $i < count($tokens) ; $i++){
			$str = $tokens[$i];
			if(is_numeric($str)){
				$volume_name = $tokens[$i-1];
				//echo "volume_name " . $volume_name . $brnl;
				$temp = explode(":", $volume_name);
				$volume_name = $temp[0];
				//echo "volume_name " . $volume_name . $brnl;
				$temp = explode(" ", $volume_name);
				$volume_name = $temp[count($temp)-1];
				//echo "volume_name " . $volume_name . $brnl;

				$total = trim($tokens[$i]);
				$total = kilo2mega($total);
				$used = trim($tokens[$i+1]);
				$used = kilo2mega($used);
				$free = trim($tokens[$i+2]);
				$free = kilo2mega($free);
				$volumes[$volume_name] = array($free, $used);
				break;
			}
		}
		}
	}


	echo "<h2>Ram</h2>";

	$div_name = "ram";

	$w = 350;
	$h = 350;

	$i = 0;
	foreach($volumes as $key=>$value){

		//if($i%2==1){ echo "<tr>"; }

		$name = "graphram" . $i;
		$data = array();
		$index1 = "Free " . $value[0] . " GB";
		$index2 = "Used " . $value[1] . " GB";
		$data[$index1] = $value[0];
		$data[$index2] = $value[1];
		$title = $key;
		initPieChart($title, $name, $div_name, $columns, $data, null, $w, $h);

		//echo "<td>";
		printChart($div_name);
		//echo "</td>";

		//if($i%2==0){ echo "</tr>"; }
		$i++;
	}
}


function printDiskSpace(){
	global $brnl;
	echo "<h2>Disk space</h2>";

	$w = 350;
	$h = 350;
	$div_name = "space";
	$columns = array();
	$columns["label1"] = "string";
	$columns["label2"] = "number";


	// passthru() function should be used in place of exec() or system() when the output from the Unix command is binary data
	// system() prints the output of the command
	// exec() returns only the last line of the output

	$volumes = array();
	$cmd = "df";
	$temp = shell_exec($cmd);

	$volumes = array();
	$tokens = preg_split("/[\s,]+/", $temp);
	for ($i=0 ; $i < count($tokens) ; $i++){
		$str = $tokens[$i];
		if(is_numeric($str)){
			//echo "numeric: " . $str . $brnl;

			$volume_name = trim($tokens[$i-1]);
			//echo "volume_name: " . $volume_name . $brnl;
			$path = explode("/", $volume_name);
			$volume_name = $path[count($path)-1];
			//echo "volume_name: " . $volume_name . $brnl;
			$total = trim($tokens[$i]);
			$total = kilo2mega($total);
			//echo "total: " . $total . $brnl;
			$used = trim($tokens[$i+1]);
			$used = kilo2mega($used);
			$free = trim($tokens[$i+2]);
			$free = kilo2mega($free);
			$volumes[$volume_name] = array($free, $used);
			$i = $i+3; // skip other numeric data
		}
	}


	//echo "<table border=\"0\">";

			//print_r($volumes);
			$i = 1;
			foreach($volumes as $key=>$value){

				//if($i%2==1){ echo "<tr>"; }

				$name = "graphdisk" . $i;
				$data = array();
				$index1 = "Free " . $value[0] . " GB";
				$index2 = "Used " . $value[1] . " GB";
				$data[$index1] = $value[0];
				$data[$index2] = $value[1];
				$title = $key;
				initPieChart($title, $name, $div_name, $columns, $data, null, $w, $h);

				//echo "<td>";
				printChart($div_name);
				//echo "</td>";

				//if($i%2==0){ echo "</tr>"; }
				$i++;
			}

	//echo "</table>";

}


//printBarChart("GRAFICO DI TEST", "graph1", $columns, $data, null);


function printMain(){
	echo "<h2>Info</h2>";

	echo "<div>";
	echo "<ul>";
	echo "<li>";
	echo "Operating system: " . getLinuxVersion();
	echo "</li>";
	echo "<li>";
	echo "Cpu info: " . getCpuinfo();
	echo "</li>";
	echo "<li>";
	echo "Host name: " . getHostName();
	echo "</li>";
	echo "<li>";
	echo "Ip address: " . getLanIp() . " (<a href=\"?action=netint\">more info</a>)";
	echo "</li>";
	echo "<li>";
	echo "Web server: " . getWebServerVersion();
	echo "</li>";
	echo "<ul>";
	echo "</div>";
}


function getCpuInfo(){
	//$r = shell_exec("cat /proc/cpuinfo | grep \"model name\\|processor\"");
	$pattern = "model name";
	$r = shell_exec("cat /proc/cpuinfo | grep \"" . $pattern . "\"");
	$r = str_replace($pattern, "", $r);
	$r = str_replace(":", "", $r);
	$r = trim($r);
	return $r;
}


function mergeArray($array1, $array2){
	$array3 = array();
	foreach($array1 as $a1){
		$array3[] = $a1;
	}
	foreach($array2 as $a2){
		$array3[] = $a2;
	}
	return $array3;
}






function kilo2mega($k){
	$m = ceil($k/1024);
	return $m;
}

function mega2giga($m){
	$g = ceil($m/1024);
	return $g;
}

?>